package com.iflytek.exam.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.iflytek.xfapi.checkCompliance.checkPicture.utils.api.CheckPicture;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.iflytek.common.annotation.Log;
import com.iflytek.common.core.controller.BaseController;
import com.iflytek.common.core.domain.AjaxResult;
import com.iflytek.common.enums.BusinessType;
import com.iflytek.exam.domain.TbQuestionSummary;
import com.iflytek.exam.service.ITbQuestionSummaryService;
import com.iflytek.common.utils.poi.ExcelUtil;
import com.iflytek.common.core.page.TableDataInfo;

/**
 * 题库管理Controller
 *
 * @author Dogdog
 * @date 2024-07-06
 */
@RestController
@RequestMapping("/exam/summary")
public class TbQuestionSummaryController extends BaseController
{
    @Autowired
    private ITbQuestionSummaryService tbQuestionSummaryService;

    /**
     * 查询题库管理列表
     */
    @PreAuthorize("@ss.hasPermi('exam:summary:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbQuestionSummary tbQuestionSummary)
    {
        startPage();
        List<TbQuestionSummary> list = tbQuestionSummaryService.selectTbQuestionSummaryList(tbQuestionSummary);
        return getDataTable(list);
    }

    /**
     * 导出题库管理列表
     */
    @PreAuthorize("@ss.hasPermi('exam:summary:export')")
    @Log(title = "题库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbQuestionSummary tbQuestionSummary)
    {
        List<TbQuestionSummary> list = tbQuestionSummaryService.selectTbQuestionSummaryList(tbQuestionSummary);
        ExcelUtil<TbQuestionSummary> util = new ExcelUtil<TbQuestionSummary>(TbQuestionSummary.class);
        util.exportExcel(response, list, "题库管理数据");
    }

    /**
     * 获取题库管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('exam:summary:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbQuestionSummaryService.selectTbQuestionSummaryById(id));
    }

    /**
     * 新增题库管理
     */
    @PreAuthorize("@ss.hasPermi('exam:summary:add')")
    @Log(title = "题库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbQuestionSummary tbQuestionSummary) throws Exception {
        //校验图片是否合规
        String result = CheckPicture.PictureCheck(tbQuestionSummary.getImg());
        if (!result.equals("pass")){
            return AjaxResult.error(result);
        }
        return toAjax(tbQuestionSummaryService.insertTbQuestionSummary(tbQuestionSummary));
    }

    /**
     * 修改题库管理
     */
    @PreAuthorize("@ss.hasPermi('exam:summary:edit')")
    @Log(title = "题库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbQuestionSummary tbQuestionSummary)
    {
        return toAjax(tbQuestionSummaryService.updateTbQuestionSummary(tbQuestionSummary));
    }

    /**
     * 删除题库管理
     */
    @PreAuthorize("@ss.hasPermi('exam:summary:remove')")
    @Log(title = "题库管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int i = tbQuestionSummaryService.deleteTbQuestionSummaryByIds(ids);
        if (i == 10000){
            return warn("该题库下存在试题，请先删除试题！");
        }else {
            return toAjax(i);
        }

    }
}
